package cs.gui;


/**
 * File MainFrame.java 
 */


import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

/**
 * Main window of the application containing the list of users 
 * and a history of actions performed on the application.
 * <p>
 * Package  cs.gui
 * Project ChatSystemG1 - INSA 4INFOA1 - 2013/2014
 * @author Julie DUMONS & Juste OUEDRAOGO
 */
@SuppressWarnings("serial")
public class MainFrame extends JFrame {

	private JLabel  lblInfo ; 

	private JPanel contentPane;

	private JList list ; 

	private JButton btnDeconnect ;

	private JTextPane txtPaneInfos ;

	private JButton btnDiscuter ; 

	private Style defaut;
	private Style errorStyle;

	private Style helloStyle;
	private StyledDocument sDoc  ; 
	private int pos=0 ;

	private Style goodbyeStyle; 
	private JScrollPane scrollPane;
	private JLabel lblHistoriqueDuChat;
	
	/**
	 * Create the frame.
	 */
	public MainFrame() {
		initGUI();

	}
	private void initGUI() {
		
		
		setTitle("Liste des utilisateurs connect\u00E9s");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 673, 440);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		JLabel lblListeDesUtilisateurs = new JLabel("Utilisateurs connect\u00E9s");
		lblListeDesUtilisateurs.setFont(new Font("Lucida Grande", Font.ITALIC, 13));
		lblListeDesUtilisateurs.setForeground(UIManager.getColor("Button.disabledText"));

		btnDiscuter = new JButton("Ouvrir une discussion");
		btnDiscuter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		btnDiscuter.setIcon(new ImageIcon("src/cs/gui/images/comment_user_add_32.png"));
		btnDiscuter.setEnabled(true);


		btnDeconnect = new JButton("Se d\u00E9connecter");
		btnDeconnect.setIcon(new ImageIcon("src/cs/gui/images/user_close_32.png"));
		btnDeconnect.setBackground(UIManager.getColor("Button.select"));
		btnDeconnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});

		lblInfo = new JLabel("youhou");
		lblInfo.setFont(new Font("Lucida Grande", Font.BOLD, 14));
		lblInfo.setHorizontalAlignment(SwingConstants.CENTER);
		lblInfo.setForeground(UIManager.getColor("CheckBoxMenuItem.selectionBackground"));
		
		JSplitPane splitPane_1 = new JSplitPane();
		splitPane_1.setOneTouchExpandable(true);
		
		JTextPane textPane = new JTextPane();
		
		lblHistoriqueDuChat = new JLabel("Historique");
		lblHistoriqueDuChat.setForeground(Color.GRAY);
		lblHistoriqueDuChat.setFont(new Font("Lucida Grande", Font.ITALIC, 13));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(19)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(btnDiscuter)
							.addContainerGap())
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addComponent(lblListeDesUtilisateurs)
									.addPreferredGap(ComponentPlacement.RELATED, 375, Short.MAX_VALUE)
									.addComponent(lblHistoriqueDuChat, GroupLayout.PREFERRED_SIZE, 82, GroupLayout.PREFERRED_SIZE)
									.addGap(20))
								.addGroup(gl_contentPane.createSequentialGroup()
									.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
										.addComponent(btnDeconnect)
										.addComponent(splitPane_1, GroupLayout.DEFAULT_SIZE, 611, Short.MAX_VALUE))
									.addPreferredGap(ComponentPlacement.RELATED)))
							.addComponent(textPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(27))))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblInfo, GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)
					.addContainerGap())
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(26)
					.addComponent(lblInfo)
					.addGap(10)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(192)
							.addComponent(textPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblListeDesUtilisateurs)
								.addComponent(lblHistoriqueDuChat, GroupLayout.PREFERRED_SIZE, 16, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(splitPane_1, GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE)))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnDiscuter)
						.addComponent(btnDeconnect))
					.addGap(20))
		);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane_1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		splitPane_1.setRightComponent(scrollPane_1);
		
				txtPaneInfos = new JTextPane();
				scrollPane_1.setViewportView(txtPaneInfos);
				txtPaneInfos.setEditable(false);
				txtPaneInfos.setBorder(new EmptyBorder(5, 5, 5, 5));
				txtPaneInfos.setEnabled(true);
				txtPaneInfos.setText("Bienvenue dans notre chat !");
				
				scrollPane = new JScrollPane();
				scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
				splitPane_1.setLeftComponent(scrollPane);
				list = new JList();
				list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
				scrollPane.setViewportView(list);
				list.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
				
				defaut = txtPaneInfos.getStyle("default");
				errorStyle = txtPaneInfos.addStyle("errorStyle", defaut);
				helloStyle = txtPaneInfos.addStyle("style2", errorStyle);
				goodbyeStyle = txtPaneInfos.addStyle("style2", errorStyle);
				
				
						 sDoc = (StyledDocument)txtPaneInfos.getDocument();
		contentPane.setLayout(gl_contentPane);

		StyleConstants.setForeground(errorStyle, new Color(0xF63D2A));
		StyleConstants.setForeground(helloStyle, new Color(0x20BA42));
		StyleConstants.setForeground(goodbyeStyle, new Color(0x265EA1));

	}

	/**
	 * @return btnDeconnect
	 */
	public JButton getBtnDeconnect() {
		return btnDeconnect;
	}
	/**
	 * 
	 * @return list Jlist containing the userConnected
	 */
	public JList getList() {
		return list;
	}


	public void setLblInfoUsername(String newText) {
		lblInfo.setText("Vous êtes connecté avec le pseudo "+newText);
	}

	
	public JButton getBtnDiscuter() {

		return btnDiscuter;
	}
	
	public void notifyUser(String text, char format) {

		try {

		switch (format) {
		case 'e' :
				sDoc.insertString(pos, text+"\n", errorStyle);pos+=text.length();
				pos+="\n".length();
			
			break ; 
		case 'h' : 
			sDoc.insertString(pos, text+"\n", helloStyle);pos+=text.length();
			pos+="\n".length();
			break;
		case 'g' : 
			sDoc.insertString(pos, text+"\n", goodbyeStyle);pos+=text.length();
			pos+="\n".length();
			break;
		default : 
			sDoc.insertString(pos, text+"\n", defaut);pos+=text.length();
			pos+="\n".length();
		}
		} catch (BadLocationException e) { }			

	}

}


